module.exports = Behavior({
  lifetimes: {
    created() {
      this._originalSetData = this.setData
      this.setData = this._setData
    }
  },
  definitionFilter(defFields) {
    const computed = defFields.computed || {}
    const computedKeys = Object.keys(computed)
    const computedCached = {}
    const calcComputed = (scope, insertToData) => {
      const needUpdate = {}
      const data = defFields.data = defFields.data || {}
      for (let key of computedKeys) {
        const value = computed[key].call(scope)
        if (computedCached[key] !==value) needUpdate[key] = computedCached[key] = value
        if (insertToData) data[key] = needUpdate[key]
      }
      return needUpdate
    }
    defFields.methods = defFields.methods || {}
    defFields.methods._setData = function(data, callback) {
      const originalSetData =  this._originalSetData
      originalSetData.call(this, data, callback)
      const needUpdate = calcComputed(this)
      originalSetData.call(this, needUpdate)
    }
    calcComputed(defFields, true)
  }
})